<template>
  <div class="worktask-designer-container">
    <top-tip :tipcontent="tipcontent" />
    <!-- <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form> -->
    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="待分配客满" name="waitAssign">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            prop="CustomerID"
            label="客户ID"
            width="70"
          />
          <el-table-column
            align="center"
            prop="CustomerAllName"
            label="客户全称"
            width="200"
          />
          <el-table-column align="center" label="分单状态" width="150">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="getTackinfo(row)">
                专
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: right; padding-top: 20px">
          <el-button type="primary" @click="batchAllocation">
            批量分配客满专员
          </el-button>
        </div>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      v-model="batchAllocationDialog"
      title="批量分配客满专员"
      width="500px"
      @close="batchAllocationClose"
    >
      <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="选择的客户ID" prop="title">
          <div>
            <span v-for="(item, index) in WorkTaskIDList" :key="index">
              {{ item }}
            </span>
          </div>
        </el-form-item>
        <el-form-item label="客满人员" prop="title">
          <el-select
            v-model="isValue"
            placeholder="请选择客满人员"
            size="large"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="batchAllocationClose">取 消</el-button>
        <el-button type="primary" @click="onBatchAllocation">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/storeOperate'
  import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'SatisfactionAssign',
    components: { TopTip },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        options: [
          {
            label: '老大',
            value: 1,
          },
          {
            label: '老二',
            value: 2,
          },
          {
            label: '老三',
            value: 3,
          },
        ],
        isValue: null,
        batchAllocationDialog: false,
        tipcontent: {
          title: '客满管理',
          memo: '本页用于“客满人员或客满管理人员”管理所管辖的业务，进行客满服务；',
        },
        WorkTaskIDList: [],
        clickList: {},
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        dialogFormVisible: false,
        selectRows: '',
        queryForm: {
          tab: 'waitAssign',
          AssignOrderSetID: 3,
          TaskState: 0,
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
        console.log(state.selectRows, '11111')
      }
      const getTackinfo = (row) => {
        router.push({
          path: '/business/shopOperate/storeDetails',
          query: { OrderID: row.BusinessOrderID, WorkTaskID: row.WorkTaskID },
        })
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const handleTabClick = (tab) => {
        if (tab.props.name == 'waitAssign') {
          state.list = [
            {
              WorkTaskID: 1,
              AddTime: '2024/8/24',
              AssignStaffName: '张三',
              CustomerAllName: '李四',
              ProductName: '产品',
              BindStoreAccount: '产品账户',
              TaskStaffName: '接单人！',
              ServiceState: 2,
            },
            {
              WorkTaskID: 2,
              AddTime: '2024/8/24',
              AssignStaffName: '张三',
              CustomerAllName: '李四',
              ProductName: '产品',
              BindStoreAccount: '产品账户',
              TaskStaffName: '接单人！',
              ServiceState: 2,
            },
            {
              WorkTaskID: 3,
              AddTime: '2024/8/24',
              AssignStaffName: '张三',
              CustomerAllName: '李四',
              ProductName: '产品',
              BindStoreAccount: '产品账户',
              TaskStaffName: '接单人！',
              ServiceState: 2,
            },
            {
              WorkTaskID: 4,
              AddTime: '2024/8/24',
              AssignStaffName: '张三',
              CustomerAllName: '李四',
              ProductName: '产品',
              BindStoreAccount: '产品账户',
              TaskStaffName: '接单人！',
              ServiceState: 2,
            },
          ]
        } else {
          console.log(tab.props.name)
          state.queryForm.tab = tab.props.name
          fetchData()
        }
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }

      const toRecord = (row) => {
        state.clickList = row
        state.dialogFormVisible = true
      }
      // 领取任务
      const receiveTask = async (row) => {
        console.log(row, state.clickList, '1111')
        if (!row.SeviceDates) {
          $baseMessage('请选择服务期限', 'error', 'vab-hey-message-error')
        } else {
          const dataMsg = {
            WorkTaskID: state.clickList.WorkTaskID,
            SeviceDates: row.SeviceDates,
          }
          const { msg } = await OperateGetOrder(dataMsg)
          $baseMessage(msg, 'success', 'vab-hey-message-success')
          state.dialogFormVisible = false
          state.clickList = {}
          fetchData()
        }
      }
      const batchAllocation = () => {
        if (state.selectRows.length == 0) {
          $baseMessage(
            '至少选择一个待分配客满',
            'error',
            'vab-hey-message-error'
          )
          return
        } else {
          state.batchAllocationDialog = true
          state.WorkTaskIDList = state.selectRows.map((item) => item.WorkTaskID)
          console.log(state.WorkTaskIDList, ' state.WorkTaskIDList')
        }
      }
      // 关闭批量分配客满弹窗
      const batchAllocationClose = () => {
        state.batchAllocationDialog = false
        state.WorkTaskIDList = []
      }
      // 批量分配
      const onBatchAllocation = () => {}

      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        toRecord,
        getTackinfo,
        batchAllocationClose,
        handleTabClick,
        handleEdit,
        onBatchAllocation,
        batchAllocation,
        receiveTask,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
